package com.dfiov.cloud.msmtools.mapper.entity.msbluetooth;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.dfiov.cloud.msmtools.contant.msbluetooth.BleAuthStatusEnum;
import com.dfiov.cloud.msmtools.contant.msbluetooth.BleKeyStatusEnum;
import com.dfiov.cloud.msmtools.entity.vo.msbluetooth.KeyPermission;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang.StringUtils;

import java.time.LocalDateTime;

@Data
public class BluetoothKeyCompleteInfoD53 {
	@TableId(value = "id", type = IdType.AUTO)
	private Long id;

	@ApiModelProperty("设备id")
	private String deviceId;

	@ApiModelProperty("设备公钥")
	private String devicePublicKey;

	@ApiModelProperty("tbox蓝牙名称")
	private String bluetoothConName;

	@ApiModelProperty("tbox蓝牙连接key")
	private String bluetoothConKey;

	@ApiModelProperty("tbox蓝牙访问key")
	private String bluetoothAccessKey;

	@ApiModelProperty("到期时间")
	private LocalDateTime keyExpireTime;

	@ApiModelProperty("用户账号sid")
	private Long userAccountSid;

	@ApiModelProperty("移动设备号")
	private String mobileDeviceNo;

	@ApiModelProperty("用户公钥")
	private String userPublicKey;

	@ApiModelProperty("钥匙关系 1 车主 0 非车主")
	private Integer relationship;

	@ApiModelProperty("钥匙状态1有效、2注销")
	private Integer keyStatus;

	@ApiModelProperty("创建时间")
	private String createtime;

	@ApiModelProperty("app端本地id")
	private String bleLocalKeyId;

	@ApiModelProperty("app端蓝牙钥匙状态 0 临时钥匙 1 永久钥匙")
	private Integer bleLocalStatus;

	@ApiModelProperty("钥匙权限")
	private KeyPermission permission;

	@ApiModelProperty("借车人手机号")
	private String borrowerPhone;

	@ApiModelProperty("授权码")
	private String authCode;

	@ApiModelProperty("授权有效期")
	private LocalDateTime authExpireTime;

	@ApiModelProperty("是否已接受授权")
	private Integer isAuthAccepted;

	@ApiModelProperty("授权确认时间")
	private LocalDateTime authAcceptedTime;

	@ApiModelProperty("授权确认用户ID")
	private Long authAcceptedUser;

    @ApiModelProperty("tbox公钥加密")
    private String authVoucher;

    @ApiModelProperty("sign")
    private String authSignature;

    @ApiModelProperty("车主id")
    private String masterId;


	public int getAuthStatus() {
		if (relationship == 1 || keyStatus.equals(BleKeyStatusEnum.AVAILABLE.getValue())) {
			return BleAuthStatusEnum.KEYACTIVE.getValue();
		} else if (keyStatus.equals(BleKeyStatusEnum.UNUSE.getValue())) {
			if (StringUtils.isBlank(borrowerPhone)) {
				return BleAuthStatusEnum.SHARE.getValue();
			} else if (0 == isAuthAccepted) {
				return BleAuthStatusEnum.AUTHNOTUSED.getValue();
			} else if (0 == isAuthAccepted || authAcceptedTime != null || authAcceptedUser != null) {
				return BleAuthStatusEnum.AUTHUSED.getValue();
			}
		}
		return BleAuthStatusEnum.KEYDELETE.getValue();
	}

}
